package com.chao.common.config.dic;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.chao.common.config.dic.DicKey;
import com.chao.common.util.redis.SysRedis;
import org.springframework.util.StringUtils;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;

import static com.chao.common.util.SysConstant.DIC;

/**
 * 实体序列化解析的时候 把字典对应的值注入到实体中
 */
public class DicSerialize extends JsonSerializer<Short> implements ContextualSerializer {

    /**
     * 存储字典key的字段名
     */
    private String key;

    /**
     * 存储在字典表中的type字段
     */
    private String type;

    public DicSerialize() {
    }

    public DicSerialize(String key, String type) {
        this.key = key;
        this.type = type;
    }

    @Override
    public void serialize(Short value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        if(null == key){
            jgen.writeObject(value);
            return;
        }
        try {
            PropertyDescriptor pd = new PropertyDescriptor(key,jgen.getCurrentValue().getClass());
            Method getMethod = pd.getReadMethod();//获得get方法
            String key1 = String.valueOf(getMethod.invoke(jgen.getCurrentValue()));
            //此处为执行该Object对象的get方法
            String data = SysRedis.getValue(DIC+type+":"+key1);
            if(!StringUtils.isEmpty(data)){
                jgen.writeString(data);
                return;
            }
            JSONArray array = SysRedis.getValue(DIC+type);
            for (int j = 0; j < array.size(); j++) {
                if(key1.equals(array.getJSONObject(j).getString("dicCode"))){
                    data = array.getJSONObject(j).getString("dicValue");
                    break;
                }
            }
            jgen.writeString(data);
        } catch (Exception e) {
            jgen.writeString("");
        }

    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty != null) { // 为空直接跳过
            if (Objects.equals(beanProperty.getType().getRawClass(),Short.class)) { // 非 String 类直接跳过
                com.chao.common.config.dic.DicKey dicKey = beanProperty.getAnnotation (com.chao.common.config.dic.DicKey.class);
                if (dicKey == null) {
                    dicKey = beanProperty.getContextAnnotation(DicKey.class);
                }
                if (dicKey != null) {
                    // 如果能得到注解，就将注解的 value 传入 ImageURLSerialize
                    return new DicSerialize(dicKey.key(),dicKey.type());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(null);
    }
}
